package net.wofly.sale.activity.web;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.wofly.common.persistence.PageWrapper;
import net.wofly.common.web.BaseController;
import net.wofly.framework.util.ReturnBean;
import net.wofly.sale.activity.domain.Activity;
import net.wofly.sale.activity.domain.ActivityGift;
import net.wofly.sale.activity.service.IActivityService;
import net.wofly.sale.activity.validator.ActivityValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "/api/activity")
@Api(description = "活动模块接口")
public class ActivityController extends BaseController {
    
    private static final String DEFAULT_PAGE_NUM = "1";
    private static final String DEFAULT_PAGE_SIZE = "10";
    
    @Autowired
    IActivityService iActivityService;
    
    /**
     * 新增活动
     * <ul>
     * <li>新增一个活动，具体活动内容在ActivityGift中体现</li>
     * <li></li>
     * </ul>
     *
     * @param activity
     */
    @ApiOperation(value = "新增活动")
    @ApiImplicitParams({@ApiImplicitParam(name = "activity", value = "活动实体", required = true, dataType = "Activity",paramType = "body")})
    @RequestMapping(value = "/addActivity", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<Activity> addActivity(@RequestBody Activity activity) {
        ReturnBean<Activity> returnBean;
        String errorMessage = ActivityValidator.validateAddActivityParams(activity);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<Activity>(-1, errorMessage);
        } 
        
        errorMessage = ActivityValidator.validateActivityTime(activity);
        if (errorMessage.length() > 0) {
        	returnBean = new ReturnBean<Activity>(-1, errorMessage);
        }
        
        returnBean = iActivityService.addActivity(activity);
        
        return returnBean;
    }
    
    /**
     * @param activityID
     * @param activityGift
     */
    @ApiOperation(value = "新增赠品")
    @ApiImplicitParams({@ApiImplicitParam(name = "activityID", value = "活动ID", required = true, dataType = "String",
            paramType = "body"),
            @ApiImplicitParam(name = "activityGift", value = "活动赠品", required = true, dataType = "ActivityGift")})
    @RequestMapping(value = "/addActivityGift", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<List<ActivityGift>> addActivityGift(String activityID, @RequestBody ActivityGift activityGift) {
        ReturnBean<List<ActivityGift>> returnBean;
        String errorMessage = ActivityValidator.validateAddActivityGift(activityID, activityGift);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<List<ActivityGift>>(-1, errorMessage);
        } else {
            returnBean = iActivityService.addActivityGift(activityID, activityGift);
        }
        return returnBean;
    }
    
    /**
     * @param activityID
     */
    @ApiOperation(value = "删除活动")
    @ApiImplicitParams({@ApiImplicitParam(name = "activityID", value = "活动ID", required = true, dataType = "String",
            paramType = "form")})
    @RequestMapping(value = "/delActivity", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<String> delActivity(String activityID) {
        ReturnBean<String> returnBean;
        StringBuffer errorMessage = ActivityValidator.validateActivityID(activityID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<String>(-1, errorMessage.toString());
        } else {
            returnBean = iActivityService.delActivity(activityID);
        }
        return returnBean;
    }
    
    /**
     * 获取所有活动
     * <ul>
     * <li></li>
     * </ul>
     *
     * @param status
     * @param pageSize
     * @param pageNum
     */
    @ApiOperation(value = "获取所有活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "activityName", value = "活动名称", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "long", paramType = "form"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageNum", value = "页码（默认第一页）", required = true, dataType = "int", paramType =
                    "form"), @ApiImplicitParam(name = "pageSize", value = "每页显示条数（默认10条）", required = true, dataType
            = "int", paramType = "form")})
    @RequestMapping(value = "/getAllActivity", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<PageWrapper<Activity>> getAllActivity(
            String activityName, Long startTime, Integer status,
            @RequestParam(defaultValue = DEFAULT_PAGE_NUM) Integer pageNum,
            @RequestParam(defaultValue = DEFAULT_PAGE_SIZE) Integer pageSize) {
        ReturnBean<PageWrapper<Activity>> returnBean = iActivityService.getAllActivity(activityName, startTime, status,
                pageNum, pageSize);
        return returnBean;
    }
    
    /**
     * 获取指定商品的活动
     * <ul>
     * <li>只取在开始与结束时间范围内，且是有效的活动</li>
     * <li>取得的活动包括ActivityGift内容</li>
     * </ul>
     *
     * @param commodityChannelID
     */
    @ApiOperation(value = "获取指定商品的活动")
    @ApiImplicitParams({@ApiImplicitParam(name = "commodityChannelID", value = "商品渠道ID", required = true, dataType =
            "String", paramType = "form")})
    @RequestMapping(value = "/getCommodityActivity", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<List<Activity>> getCommodityActivity(String commodityChannelID) {
        ReturnBean<List<Activity>> returnBean;
        String errorMessage = ActivityValidator.validateCommodityChannelID(commodityChannelID).toString();
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<List<Activity>>(-1, errorMessage);
        } else {
            returnBean = iActivityService.getCommodityActivity(commodityChannelID);
        }
        return returnBean;
    }
    
    /**
     * 修改活动
     * <ul>
     * <li>只有当活动未生效时，可以修改，一旦生效则不可以修改</li>
     * <li></li>
     * </ul>
     *
     * @param activity
     */
    @ApiOperation(value = "修改活动")
    @ApiImplicitParams({@ApiImplicitParam(name = "activity", value = "活动实体", required = true, dataType = "Activity",paramType = "body")})
    @RequestMapping(value = "/modifyActivity", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<Activity> modifyActivity(@RequestBody Activity activity) {
        ReturnBean<Activity> returnBean;
        String errorMessage = ActivityValidator.validateModifyActivityParams(activity);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<Activity>(-1, errorMessage);
        } else {
            returnBean = iActivityService.modifyActivity(activity);
        }
        return returnBean;
    }
    
    /**
     * 批量添加赠品
     *
     * @param activityID
     * @param activityGifts
     */
    @ApiOperation(value = "批量添加赠品")
    @ApiImplicitParams({@ApiImplicitParam(name = "activityID", value = "活动ID", required = true, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "activityGifts", value = "活动实体集合", required = true, dataType = "List")})
    @RequestMapping(value = "/addActivityGifts", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<List<ActivityGift>> addActivityGifts(String activityID, @RequestBody String activityGifts) {
        ReturnBean<List<ActivityGift>> returnBean;
        List<ActivityGift> gifts = JSON.parseArray(activityGifts, ActivityGift.class);
        String errorMessage = ActivityValidator.validateAddActivityGifts(activityID, gifts);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<List<ActivityGift>>(-1, errorMessage);
        } else {
            returnBean = iActivityService.addActivityGifts(activityID, gifts);
        }
        return returnBean;
    }
    
    /**
     * 获取活动赠品
     *
     * @param activityID
     */
    @ApiOperation(value = "获取活动赠品")
    @ApiImplicitParams({@ApiImplicitParam(name = "activityID", value = "活动ID", required = true, dataType = "String", paramType = "form")})
    @RequestMapping(value = "/getActivityGift", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<List<ActivityGift>> getActivityGift(String activityID) {
        ReturnBean<List<ActivityGift>> returnBean;
        String errorMessage = ActivityValidator.validateActivityID(activityID).toString();
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<List<ActivityGift>>(-1, errorMessage);
        } else {
            returnBean = iActivityService.getActivityGift(activityID);
        }
        return returnBean;
    }
    
    /**
     * 发布活动
     *
     * @param activityID
     * @return
     */
    @ApiOperation(value = "发布活动")
    @ApiImplicitParams({@ApiImplicitParam(name = "activityID", value = "活动ID", required = true, dataType = "String", paramType = "form")})
    @RequestMapping(value = "/publishActivity", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<Activity> publishActivity(String activityID) {
        ReturnBean<Activity> returnBean;
        String errorMessage = ActivityValidator.validateActivityID(activityID).toString();
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<Activity>(-1, errorMessage);
        } else {
            returnBean = iActivityService.publishActivity(activityID);
        }
        return returnBean;
    }
    
    /**
     * 下线活动
     *
     * @param activityID
     * @return
     */
    @ApiOperation(value = "活动下线")
    @ApiImplicitParams({@ApiImplicitParam(name = "activityID", value = "活动ID", required = true, dataType = "String", paramType = "form")})
    @RequestMapping(value = "/offlineActivity", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<Activity> offlineActivity(String activityID) {
        ReturnBean<Activity> returnBean;
        String errorMessage = ActivityValidator.validateActivityID(activityID).toString();
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<Activity>(-1, errorMessage);
        } else {
            returnBean = iActivityService.offlineActivity(activityID);
        }
        return returnBean;
    }
    
    /**
     * 活动详情
     *
     * @param activityID
     * @return
     */
    @ApiOperation(value = "活动详情")
    @ApiImplicitParams({@ApiImplicitParam(name = "activityID", value = "活动ID", required = true, dataType = "String", paramType = "form")})
    @RequestMapping(value = "/getActivityByID", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<Activity> getActivityByID(String activityID) {
        ReturnBean<Activity> returnBean;
        String errorMessage = ActivityValidator.validateActivityID(activityID).toString();
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<Activity>(-1, errorMessage);
        } else {
            returnBean = iActivityService.getActivityByID(activityID) ;
        }
        return returnBean;
    }
    
    /**
     * 获取指定订单类型及活动类型的活动
     * <ul>
     * <li></li>
     * </ul>
     *
     * @param status
     * @param pageSize
     * @param pageNum
     */
    @ApiOperation(value = "获取指定订单类型及活动类型的活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderType", value = "订单类型", dataType = "Integer", paramType = "form"),
            @ApiImplicitParam(name = "activityType", value = "活动类型", dataType = "Integer", paramType = "form")})
    @RequestMapping(value = "/getActivityByOrderTypeAndActivityType", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<List<Activity>> getActivityByOrderTypeAndActivityType(Integer orderType, Integer activityType) {
        ReturnBean<List<Activity>> returnBean = iActivityService.getActivityByOrderTypeAndActivityType(orderType, activityType) ;
        return returnBean;
    }
    
}
